package com.micolor.logr.demo.controller;


import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.micolor.logr.demo.dto.UserDTO;
import com.micolor.logr.demo.entity.UserCash;
import com.micolor.logr.demo.entity.UserInfo;
import com.micolor.logr.demo.service.IUserCashService;
import com.micolor.logr.demo.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * @author EvanGe
 * @Type UserController.java
 * @Desc
 * @date 2024/7/8 11:07
 */
@Controller
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IUserCashService userCashService;

    @PostMapping("/getUserList")
    @ResponseBody
    public List getUserInfo(){
        return userService.list();
    }
    @PostMapping("/saveUserInfo")
    @ResponseBody
    @Transactional
    public JSONObject saveUserInfo(@RequestBody UserInfo userInfo) throws Exception {
        userInfo.setCreateBy("U0001");
        userInfo.setCreateByName("超级管理员");
        userInfo.setCreateTime(new Date());
        UserInfo newUser = userService.saveUserInfo(userInfo);

        UserCash userCash = new UserCash();
        userCash.setUserId(newUser.getUserId());
        userCash.setUserCash(0.0F);
        userCash.setCreateBy("U0001");
        userCash.setCreateByName("超级管理员");
        userCash.setCreateTime(new Date());
        userCashService.saveUserInfo(userCash);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userInfo",newUser);
        jsonObject.put("userCash",userCash);

        return jsonObject;
    }

    @PostMapping("/updateUserInfo")
    @ResponseBody
    public JSONObject updateUserInfo(@RequestBody UserDTO userDTO) throws Exception{
        UserInfo userInfo = userDTO.getUserInfo();
        //将现在的数据添加上更新操作的一些信息
        userInfo.setUpdateBy("U0005");
        userInfo.setUpdateByName("管理员");
        userInfo.setUpdateTime(new Date());
        UserInfo newUser = userService.updateUserInfo(userInfo);

        UserCash userCash = userDTO.getUserCash();
        //查询原来的钱包数据
        LambdaQueryWrapper<UserCash> qwCash = new LambdaQueryWrapper<>();
        qwCash.eq(UserCash::getUserId,userInfo.getUserId());
        UserCash oldCash =  userCashService.getOne(qwCash);

        userCash.setUpdateBy("U0005");
        userCash.setUpdateByName("管理员");
        userCash.setUpdateTime(new Date());
        userCashService.updateUserInfo(oldCash,userCash);


        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userInfo",newUser);
        jsonObject.put("userCash",userCash);
        return jsonObject;
    }

    @PostMapping("/deleteUserInfo")
    @ResponseBody

    public JSONObject deleteUserInfo(@RequestBody UserInfo userInfo){
        if(userInfo!=null){
            userInfo.setUpdateBy("U0002");
            userInfo.setUpdateByName("运维人员");
            userInfo.setUpdateTime(new Date());
            userService.deleteUserInfo(userInfo);
        }

        //查询原来的钱包数据
        LambdaQueryWrapper<UserCash> qwCash = new LambdaQueryWrapper<>();
        qwCash.eq(UserCash::getUserId,userInfo.getUserId());
        UserCash userCash =  userCashService.getOne(qwCash);
        if(userCash!=null){
            userCash.setUpdateBy("U0002");
            userCash.setUpdateByName("运维人员");
            userCash.setUpdateTime(new Date());
            userCashService.deleteUserInfo(userCash);
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userInfo",userInfo);
        jsonObject.put("userCash",userCash);
        return jsonObject;
    }
}
